package com.cv.system.dal.mapper.user;

import cn.hutool.core.util.ObjUtil;
import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.mybatis.core.mapper.BaseMapperX;
import com.cv.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.system.controller.system.user.vo.user.UserPageReqVO;
import com.cv.system.controller.system.user.vo.user.UserRespVO;
import com.cv.system.dal.dataobject.user.SystemUsersDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 系统用户数据层
 *
 * @author Charles_XDXD
 */
@Mapper
public interface SystemUserMapper extends BaseMapperX<SystemUsersDO> {

    /**
     * 根据用户账号查询用户
     *
     * @param username 用户账号
     * @return 用户信息
     */
    default SystemUsersDO selectByUsername(String username) {
        return selectOne(SystemUsersDO::getUsername, username);
    }

    /**
     * 根据邮箱查询用户
     *
     * @param email 邮箱
     * @return 用户信息
     */
    default SystemUsersDO selectByEmail(String email) {
        return selectOne(SystemUsersDO::getEmail, email);
    }

    /**
     * 根据手机号查询用户
     *
     * @param mobile 手机号
     * @return 用户信息
     */
    default SystemUsersDO selectByMobile(String mobile) {
        return selectOne(SystemUsersDO::getMobile, mobile);
    }

    default PageResult<UserRespVO> selectUserRespPage(UserPageReqVO pageReqVO) {
        MPJLambdaWrapperX<SystemUsersDO> wrapper = new MPJLambdaWrapperX<SystemUsersDO>()
                .selectAll(SystemUsersDO.class)
                .likeIfPresent(SystemUsersDO::getUsername, pageReqVO.getUsername())
                .likeIfPresent(SystemUsersDO::getNickname, pageReqVO.getNickname())
                .likeIfPresent(SystemUsersDO::getMobile, pageReqVO.getMobile())
                .likeIfPresent(SystemUsersDO::getEmail, pageReqVO.getEmail())
                .eqIfPresent(SystemUsersDO::getStatus, pageReqVO.getStatus())
                .betweenIfPresent(SystemUsersDO::getLoginDate, pageReqVO.getLoginDate())
                .betweenIfPresent(SystemUsersDO::getCreateTime, pageReqVO.getCreateTime())
                .orderByDesc(SystemUsersDO::getId);

        if (ObjUtil.isNotEmpty(SecurityFrameworkUtil.getLoginUserId())) {
            wrapper.ne(SystemUsersDO::getId, SecurityFrameworkUtil.getLoginUserId());
        }

        return selectJoinPage(pageReqVO, UserRespVO.class, wrapper);
    }

}
